//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "USRegion.h"
using namespace Cephei::QL::Indexes;
#include <gen/QL/Indexes/Region.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::CUSRegion::CUSRegion () : CRegion(CUSRegion::typeid)
{
    try
    {
#ifdef HANDLE
        _phUSRegion = NULL;
#endif
        _ppUSRegion = new boost::shared_ptr<QuantLib::USRegion> (new QuantLib::USRegion ( ));
        SetRegion (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Indexes::CUSRegion::CUSRegion (boost::shared_ptr<QuantLib::USRegion>& childNative, Object^ owner) : CRegion(CUSRegion::typeid)
{
#ifdef HANDLE
	_phUSRegion = NULL;
#endif
	_ppUSRegion = &childNative;
    _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
}
Cephei::QL::Indexes::CUSRegion::CUSRegion (QuantLib::USRegion& childNative, Object^ owner) : CRegion(CUSRegion::typeid)
{
#ifdef HANDLE
	_phUSRegion = NULL;
#endif
	_ppUSRegion = new boost::shared_ptr<QuantLib::USRegion> (&childNative);
    _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
    _USRegionOwner = owner;
    _RegionOwner = owner;
}

Cephei::QL::Indexes::CUSRegion::CUSRegion (CUSRegion^ copy) : CRegion(CUSRegion::typeid)
{
#ifdef HANDLE
	_phUSRegion = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppUSRegion = new boost::shared_ptr<QuantLib::USRegion> (copy->GetShared());
        _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
    }
}
Cephei::QL::Indexes::CUSRegion::CUSRegion (PLATFORM::Type^ t) : CRegion(CUSRegion::typeid)
{
#ifdef HANDLE
	_phUSRegion = NULL;
#endif
	if (!t->IsSubclassOf(CUSRegion::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::CUSRegion::CUSRegion (QuantLib::Handle<QuantLib::USRegion>& childNative, Object^ owner)  : CRegion(CUSRegion::typeid)
{
	_phUSRegion = &childNative;
	_ppUSRegion = &static_cast<boost::shared_ptr<QuantLib::USRegion>>(childNative.currentLink());
    _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
    _USRegionOwner = owner;
}
Cephei::QL::Indexes::CUSRegion::CUSRegion (QuantLib::Handle<QuantLib::USRegion> childNative)  : CRegion(CUSRegion::typeid)
{
	_phUSRegion = &childNative;
	_ppUSRegion = &static_cast<boost::shared_ptr<QuantLib::USRegion>>(childNative.currentLink());
    _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::CUSRegion::CUSRegion (QuantLib::USRegion childNative)  : CRegion(CUSRegion::typeid)
{
#ifdef HANDLE
	_phUSRegion = NULL;
#endif
	_ppUSRegion = new boost::shared_ptr<QuantLib::USRegion> (new QuantLib::USRegion (childNative));
    _ppRegion = new boost::shared_ptr<QuantLib::Region> (boost::dynamic_pointer_cast<QuantLib::Region> (*_ppUSRegion));
}
#endif

Cephei::QL::Indexes::CUSRegion::~CUSRegion ()
{
    if (_ppUSRegion != NULL)
    {
	    delete _ppUSRegion;
        _ppUSRegion = NULL;
    }
}
Cephei::QL::Indexes::CUSRegion::!CUSRegion ()
{
    if (_ppUSRegion != NULL)
    {
	    delete _ppUSRegion;
    }
}
QuantLib::USRegion& Cephei::QL::Indexes::CUSRegion::GetReference ()
{
    if (_ppUSRegion == NULL) throw REFNEW NativeNullException ();
	return **_ppUSRegion;
}
boost::shared_ptr<QuantLib::USRegion>& Cephei::QL::Indexes::CUSRegion::GetShared ()
{
    if (_ppUSRegion == NULL) throw REFNEW NativeNullException ();
	return *_ppUSRegion;
}
QuantLib::USRegion* Cephei::QL::Indexes::CUSRegion::GetPointer ()
{
    if (_ppUSRegion == NULL) throw REFNEW NativeNullException ();
	return &**_ppUSRegion;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::USRegion>& Cephei::QL::Indexes::CUSRegion::GetHandle ()
{
	if (_phUSRegion == NULL)
	{
		_phUSRegion = new Handle<QuantLib::USRegion> (*_ppUSRegion);
	}
	return *_phUSRegion;
}
#endif
bool Cephei::QL::Indexes::CUSRegion::HasNative () 
{
	return (_ppUSRegion != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::IUSRegion^ Cephei::QL::Indexes::CUSRegion_Factory::Create ()
{
    return REFNEW CUSRegion ();
}
